

local UI_LEFT, UI_RIGHT = 0, 0
local UI_VERTICAL_MIDDLE = (UI_LEFT + UI_RIGHT) * 0.5
local UI_TOP, UI_BOTTOM = 0, 0
local TILE_SIZE, TILE_HALFSIZE = 34, 16
local SKILLTREESTRINGS = '我是标题'

local ORDERS =
{
	{"group_1", {UI_LEFT, UI_TOP}},
	{"group_2", 	{UI_LEFT, UI_TOP}},  --类似技能树的分支，在下面的技能树上该技能分支将至少有一个技能点在该分支上
}

local function Jiaxintang_Skilltree_Change(inst)
	local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) or nil
	if weapon then
		weapon:PushEvent("Skilltree_Change")
	end
end

local function BuildSkillsData(SkillTreeFns)
    local skills = {
		a_steelcut = {
			title = "斩铁",						--标题
			desc = "desc",					--描述
			icon = "a_steelcut",						--图标，回去找xml里的同名，也一定要写同名
			pos = {UI_LEFT - 180, UI_TOP + 120 },		--坐标
			root = true,							--true表示一开始就可以选择，作为分支的根部，如果需要条件解锁，请开false
			onactivate   = function(inst) inst:AddTag("kkd_a_steelcut") end ,
			ondeactivate = function(inst) inst:RemoveTag("kkd_a_steelcut") end,			--给角色打tag
			group = "group_1",						--在order的哪个组
			tags = {"skill","a_steelcut"},				--tag数量会作为后续解锁的前提
			connects = {
				"a_dismember",'a_saw'						--表示学会该阶段后将解锁的节点
			},
		},

		a_dismember = {
			title = "肢解",
			desc = "desc",
			icon = "a_dismember",
			pos = {UI_LEFT - 200, UI_TOP + 50},
			root = false,
			onactivate   = function(inst) print('onactivate') end ,
			ondeactivate = function(inst) print('ondeactivate') end,			
			group = "group_1",
			tags = {"skill","a_dismember"},
			connects = {
				'a_adrenaline'
			},
		},

		a_adrenaline = {
			title = "肾上腺素",
			desc = "desc",
			icon = "a_adrenaline",
			pos = {UI_LEFT - 120, UI_TOP - 15 },
			root = false,
			onactivate   = function(inst) print('onactivate') end ,
			ondeactivate = function(inst) print('ondeactivate') end,
			group = "group_1",
			tags = {"skill","a_adrenaline"},
			connects = {
			},
		},

        a_saw = {
			title = "锯刃",
			desc = "desc",
			icon = "a_saw",
			pos = {UI_LEFT - 80 , UI_TOP + 80},
			root = false,
			onactivate   = function(inst) print('onactivate') end ,
			ondeactivate = function(inst) print('ondeactivate') end,
			group = "group_1",
			tags = {"skill","a_saw"},
			connects = {
			},
		},
        ---------------
		b_mechanicalpower = {
			title = "机械动力",						--标题
			desc = "desc",					--描述
			icon = "b_mechanicalpower",						--图标，回去找xml里的同名，也一定要写同名
			pos = {UI_LEFT + 150, UI_TOP + 95},		--坐标
			root = true,							--true表示一开始就可以选择，作为分支的根部，如果需要条件解锁，请开false
			onactivate   = function(inst) print('onactivate') end ,
			ondeactivate = function(inst) print('ondeactivate') end,
			group = "group_2",						--在order的哪个组
			tags = {"skill","b_mechanicalpower"},				--tag数量会作为后续解锁的前提
			connects = {
				'b_precison',
			},
		},

        b_precison = {
			title = "精密器械",
			desc = "desc",
			icon = "b_precison",
			pos = {UI_LEFT + 60, UI_TOP + 80},
			root = false,
			onactivate   = function(inst) print('onactivate') end ,
			ondeactivate = function(inst) print('ondeactivate') end,
			group = "group_1",
			tags = {"skill","b_precison"},
			connects = {
			},
		},

		-- debug_lock1 = {
		-- 	desc = "你需要学习3个技能点解锁",
		-- 	pos = {UI_LEFT + 390, UI_TOP -120},
			
		-- 	lock_open = function(prefabname, skillselection)
		-- 		return SkillTreeFns.CountTags(prefabname, "skill", skillselection) >= 3
		-- 	end,
			
		-- 	group = "debug2",
		-- 	tags = {"skill","others", "lock"},
		-- },
		-- spiderqueen_defeat_lock = {
		-- 	desc = "你需要击杀蜘蛛女王解锁解锁",
		-- 	pos = {UI_LEFT + 235, UI_TOP - 10},
			
		-- 	lock_open = function(prefabname, skillselection)
		-- 	return (skillselection ~= nil and "question")
		-- 		or (TheGenericKV:GetKV("spiderqueen_killed") == "1")
		-- 	end,
			
		-- 	group = "debug2",
		-- 	tags = {"skill", "lock"},
		-- },
	}
	for _, skill in pairs(skills) do 
		local icon = skill.icon
		skill.icon = {"images/kukude_skilltree.xml", icon..".tex"}
		if skill.connects and not next(skill.connects) then 
			skill.connects = nil 
		end
	end
	
	return {
		SKILLS = skills,
		ORDERS = ORDERS,
		BACKGROUND_PATHS = {"images/kukude_skill_background.xml", "kukude_skill_background.tex"}
	}
end

return BuildSkillsData

--[[local skilltree_defs = require("prefabs/skilltree_defs")
local avatarid = 'kukude'

if BuildSkillsData then --给角色创建一个技能树
    local data = BuildSkillsData(skilltree_defs.FN)
    if data then
        skilltree_defs.CreateSkillTreeFor(avatarid, data.SKILLS)
        skilltree_defs.SKILLTREE_ORDERS[avatarid] = data.ORDERS
    end
end


local OldGetSkilltreeBG = GLOBAL.GetSkilltreeBG
GLOBAL.GetSkilltreeBG = function(imagename,...)
	if imagename == "kukude_background.tex" then
		return  "images/kukude_skill_background.xml"       --重定位到自己的技能背景上，需要去xml里把资源名改成“esctemplate_background”
	else
		return OldGetSkilltreeBG(imagename, ...)
	end
end

local SkillTreeBuilder = require("widgets/redux/skilltreebuilder")
-- local Image = require("widgets/image")
local OldCreateTree = SkillTreeBuilder.CreateTree
function SkillTreeBuilder:CreateTree(prefabname, targetdata, readonly, ...)
	OldCreateTree(self, prefabname, targetdata, readonly, ...)
	if self.skilltreewidget.target == avatarid then
		for k, v in pairs(self.skillgraphics) do
			local skillbutton = v.button
			if skillbutton ~= nil then
				local skillicon = skillbutton:AddChild(Image("images/kukude_skilltree.xml", k..".tex"))
				skillicon:ScaleToSize(28, 28)
				skillicon:MoveToFront()
			end
		end
	end
end]]             --Icon重定位

-- AddPrefabPostInit("spiderqueen", function(inst)     --通过API给蜘蛛女王一个击杀RPC
-- 	inst.attackerUSERIDs = {}
-- 	local function trackattackers(inst, data)
-- 		if data.attacker and data.attacker:HasTag("player") then
-- 			inst.attackerUSERIDs[data.attacker.userid] = true
-- 			print(inst.attackerUSERIDs)
-- 			print("tttt")
-- 		end
-- 	end

-- 	local function OnDead(inst, data)
-- 		if inst.attackerUSERIDs ~= nil then 
-- 			for ID, data in pairs(inst.attackerUSERIDs) do
-- 				for i, player in ipairs(AllPlayers) do
-- 					if player.userid == ID then 
-- 						SendRPCToClient(CLIENT_RPC.UpdateAccomplishment, player.userid, "spiderqueen_killed")
-- 						break
-- 					end
-- 				end
-- 			end
-- 		end
-- 	end
-- 	inst:ListenForEvent("attacked", trackattackers)
-- 	inst:ListenForEvent("death", OnDead)
-- end)